package com.example.service;

import com.example.dao.ContactRecordDao;
import com.example.dao.ContactUserDao;
import com.example.dao.impl.ContactRecordDaoImpl;
import com.example.dao.impl.ContactUserDaoImpl;
import com.example.entity.customerAdmin.ContactRecord;
import com.example.entity.customerAdmin.ContactUser;
import com.example.utils.ResultData;
import lombok.Data;
import org.springframework.stereotype.Service;

import java.util.List;
@Data
@Service
public class ContactService {
    ContactUserDao contactUserDao=new ContactUserDaoImpl();
    ContactRecordDao contactRecordDao=new ContactRecordDaoImpl();
    public ResultData selectAll(int page, int limit) {
        int newPage=(page-1)*limit;
        ResultData resultData=new ResultData();
        List<ContactUser> contactUserList = contactUserDao.selectAll(newPage, limit);
        resultData.setCode(0);
        resultData.setCount(contactUserDao.getCount());
        resultData.setData(contactUserList);
        return resultData;
    }

    public ResultData deleteById(int id) {
        ResultData resultData=new ResultData();
        int i = contactUserDao.delete(id);
        if(i>0){
            resultData.setCode(200);
        }else {
            resultData.setCode(404);
        }
        return resultData;
    }

    public ResultData addContact(ContactUser contactUser) {
        ResultData resultData=new ResultData();
        int add = contactUserDao.add(contactUser);
        if(add>0){
            resultData.setCode(200);
            resultData.setMsg("添加成功");
        }else {
            resultData.setCode(404);
            resultData.setMsg("添加失败");
        }
        return resultData;
    }

    public ResultData selectConRecord(int page, int limit) {
        int newPage=(page-1)*limit;
        ResultData resultData=new ResultData();
        List<ContactRecord>contactRecordList=contactRecordDao.selectAll(newPage,limit);
        resultData.setCode(0);
        resultData.setCount(contactUserDao.getCount());
        resultData.setData(contactRecordList);
        return resultData;
    }
}
